#include "w_pushbutton.h"
#include "w_pushbuttonprivate.h"
#include <QPainter>
#include <QPainterPath>
#include <QEvent>
#include <QPropertyAnimation>

Q_PROPERTY_CREATE_Q_CPP(W_PushButton, int, BorderRadius)
Q_PROPERTY_CREATE_Q_CPP(W_PushButton, QColor, DefaultColor)
Q_PROPERTY_CREATE_Q_CPP(W_PushButton, QColor, HoverColor)
Q_PROPERTY_CREATE_Q_CPP(W_PushButton, QColor, PressColor)
W_PushButton::W_PushButton(QWidget *parent)
    : QWidget(parent)
    , d_ptr(new W_PushButtonPrivate())
{
    setWindowFlags(Qt::FramelessWindowHint |Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    //设置背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    Q_D(W_PushButton);
    d->q_ptr = this;
    d->_pBorderRadius = 6;
    d->_pDefaultColor = "#FFFFFF";
    d->_pHoverColor = "#006363";
    d->_pPressColor = "#33CCCC";
    d->_textColor = "#DCDCDC";

    setMouseTracking(true);
    // setFixedHeight(38);
    QFont font = this->font();
    font.setPixelSize(15);
    setFont(font);
    setObjectName("WPushButton");
    setStyleSheet("#WPushButton{background-color:transparent;}");
}

W_PushButton::~W_PushButton()
{

}

void W_PushButton::setTextColor(QColor color)
{
    Q_D(W_PushButton);
    d->_textColor = std::move(color);
}

QColor W_PushButton::getTextColor() const
{
    Q_D(const W_PushButton);
    return d->_textColor;
}

void W_PushButton::mousePressEvent(QMouseEvent *event)
{
    Q_D(W_PushButton);
    d->_isPressed = true;
    QWidget::mousePressEvent(event);
}

void W_PushButton::mouseReleaseEvent(QMouseEvent *event)
{
    Q_D(W_PushButton);
    d->_isPressed = false;
    QWidget::mouseReleaseEvent(event);
}

void W_PushButton::paintEvent(QPaintEvent *event)
{
    Q_D(W_PushButton);
    QPainter painter(this);
    painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing);
    // 高性能阴影
    drawEffectShadow(&painter,rect(), d->_shadowBorderWidth, d->_pBorderRadius);
    // 背景绘制
    painter.save();
    QRect foregroundRect(d->_shadowBorderWidth, d->_shadowBorderWidth, width() - 2 * (d->_shadowBorderWidth), height() - 2 * d->_shadowBorderWidth);

    painter.setPen(d->_pDefaultColor);
    painter.setBrush(isEnabled() ? d->_isPressed ? d->_pPressColor : (underMouse() ? d->_pHoverColor : d->_pDefaultColor) : d->_pDefaultColor);


    painter.drawRoundedRect(foregroundRect, d->_pBorderRadius, d->_pBorderRadius);
    // 底边线绘制
    if (!d->_isPressed)
    {
        painter.setPen(d->_pDefaultColor);
        painter.drawLine(foregroundRect.x() + d->_pBorderRadius, height() - d->_shadowBorderWidth, foregroundRect.width(), height() - d->_shadowBorderWidth);
    }
    //文字绘制
    painter.setPen(d->_textColor);
    painter.drawText(foregroundRect, Qt::AlignCenter, d->_text);
    painter.restore();
}

bool W_PushButton::event(QEvent *event)
{
    Q_D(W_PushButton);
    switch (event->type())
    {
    case QEvent::Enter:
    {
        QPropertyAnimation* scaleAnimation = new QPropertyAnimation(this, "geometry");
        scaleAnimation->setDuration(130);
        QRect startRect = geometry();
        QRect endRect(startRect.x()-2, startRect.y()-2,
                      startRect.width()+4, startRect.height()+4);
        scaleAnimation->setStartValue(startRect);
        scaleAnimation->setEndValue(endRect);
        scaleAnimation->start(QAbstractAnimation::DeleteWhenStopped);
        break;
    }
    case QEvent::Leave:
    {
        QPropertyAnimation* scaleAnimation = new QPropertyAnimation(this, "geometry");
        scaleAnimation->setDuration(130);
        QRect startRect = geometry();
        QRect endRect(startRect.x()+2, startRect.y()+2,
                      startRect.width()-4, startRect.height()-4);
        scaleAnimation->setStartValue(startRect);
        scaleAnimation->setEndValue(endRect);
        scaleAnimation->start(QAbstractAnimation::DeleteWhenStopped);
        break;
    }
    case QEvent::MouseButtonRelease:
    {
        // Q_EMIT popularCardClicked();
        break;
    }
    default:
    {
        break;
    }
    }
    return QWidget::event(event);
}

void W_PushButton::drawEffectShadow(QPainter *painter, QRect widgetRect, int shadowBorderWidth, int borderRadius)
{
    painter->save();
    painter->setRenderHints(QPainter::Antialiasing);
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
    QColor color = QColor(63, 63, 63,180);
    for (int i = 0; i < shadowBorderWidth; i++)
    {
        path.addRoundedRect(widgetRect.x() + shadowBorderWidth - i, widgetRect.y() + shadowBorderWidth - i, widgetRect.width() - (shadowBorderWidth - i) * 2, widgetRect.height() - (shadowBorderWidth - i) * 2, borderRadius + i, borderRadius + i);
        int alpha = 1 * (shadowBorderWidth - i + 1);
        color.setAlpha(alpha > 255 ? 255 : alpha);
        painter->setPen(color);
        painter->drawPath(path);
    }
    painter->restore();
}
